//(加、减环节没有for循环只有全选框、单选框、合计、确认环节有for循环)
//全选框选中（一样）
function checkboxAll() {
    var checkAllEle = document.querySelector('.checkbox-all')
    var checkList = document.querySelectorAll('.checkbox-item')
    for (var i = 0; i < checkList.length; i++) {
        if (checkAllEle.checked) {
            checkList[i].checked = true
        } else {
            checkList[i].checked = false
        }
    }
}
//单个复选框（一样）
function checkBoxSingle() {
    var checkAllEle = document.querySelector('.checkbox-all')
    var checkList = document.querySelectorAll('.checkbox-item')
    var isCheckAll = true
    for (var i = 0; i < checkList.length; i++) {
        if (checkList[i].checked == false) {
            isCheckAll = false
            break;
        }
    }
    if (isCheckAll) {
        checkAllEle.checked = true
    } else {
        checkAllEle.checked = false
    }
}
//减-
function minus(index) {
    var prices = document.getElementsByName("price")[index].value
    var num = document.getElementsByName("amount")[index].value - 1;
    if (num < 1) {
        alert("不能再减了，再减就没有啦！")
    } else {
        document.getElementsByName("amount")[index].value = num
        var totals = prices * num
        document.getElementById("price" + index).innerHTML = "￥" + totals.toFixed(2)
        total()
    }
}
//加+
function plus(index) {
    var prices = document.getElementsByName("price")[index].value;
    //这步相当于count=count+1(累加并赋值相当于 i+=1 等同于 i=i+1)
    var num = parseInt(document.getElementsByName("amount")[index].value) + 1
    document.getElementsByName("amount")[index].value = num
        //parseFloat取整且保留一个小数点
    var totals = prices * num
    document.getElementById("price" + index).innerHTML = "￥" + totals.toFixed(2)
    total()
}
//合计总结算自动生成
function total() {
    var prices = document.getElementsByName("price")
    var num = document.getElementsByName("amount")
        //这里的sum是累计加起来的总结算价格
    var sum = 0
    for (var i = 0; i < prices.length; i++) {
        sum += prices[i].value * num[i].value
    }
    document.getElementById("totalPrice").innerHTML = "￥" + sum.toFixed(2)
}
total()

//购买确认弹框
function accounts() {
    var prices = document.getElementsByName("price")
    var num = document.getElementsByName("amount")
    var sum = 0;
    for (var i = 0; i < prices.length; i++) {
        sum += prices[i].value * num[i].value
    }
    //语法confirm（）表示确认的标签名
    var flag = confirm('您已选购价值为' + "￥" + sum.toFixed(2) + '的衣服，是否结算?')
    if (flag == true) {
        alert("购买成功!");
    } else {
        alert("已取消购买!")
    }
}